/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File SubscriptionPointImpl.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.activeData;

import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.NoSuchSubscription;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.Status;
import il.ac.biu.cs.grossmm.api.flow.Subscriber;
import il.ac.biu.cs.grossmm.api.flow.SubscriptionPoint;
import il.ac.biu.cs.grossmm.api.flow.Track;
import il.ac.biu.cs.grossmm.api.keys.Key;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;

public class SubscriptionPointImpl<N> implements SubscriptionPoint<N> {
	final SubscriptionDispatcher<N> dispatcher;

	final Subscriber<N> subscriber;

	final KeyPattern pattern;

	final DataLayer<N> dataLayer;

	SubscriptionPointImpl(DataLayer<N> dataLayer, Subscriber<N> subscriber,
			KeyPattern pattern) {
		this.dispatcher = new SubscriptionDispatcher<N>(this);
		this.subscriber = subscriber;
		this.pattern = pattern;
		this.dataLayer = dataLayer;
	}

	public Status subscribe(Key key, Track track) {
		// is there a cached subscription for this key?
		SubscriptionImpl<N> s = dataLayer.tryCached(key, subscriber);

		// lock required
		if (s != null) {
			// there is a cached subscription

			// TODO: consider moving this to tryCached

			if (s.getStatus() == Status.ACTIVE) {
				// this subscription is in active state
				subscriber.activated(key, s.getRoot());
			} else {
				// this subscription is in deactivated state
				try {
					subscriber.deactivated(key, s.getStatus());
				} catch (NoSuchSubscription e) {
					// not expected unless subscriber's code is buggy
					s.remove(subscriber);
					e.printStackTrace();
					return new Status.Error(e.getMessage());
				}
			}

			// return the current status of the subscription
			return s.getStatus();
		}

		// not cached

		return dispatcher.subscribe(key, track);
	}

	public void unsubscribe(Key key) {
		dataLayer.unsubscribe(key, subscriber);
	}

	public Root<N> getRoot(Key key) {
		return dataLayer.getRoot(key);
	}

	public KeyPattern getPattern() {
		return pattern;
	}

	public NodeInspector readLock(Root<N> root) {
		return ((il.ac.biu.cs.grossmm.impl.activeData.Root<N>) root).readLock();
	}

	public void readUnlock(Root<N> root) {
		((il.ac.biu.cs.grossmm.impl.activeData.Root<N>) root).readLock();
	}

	public Root<N> fetch(Key key) {
		// TODO Implement fetch
		// return null;
		throw new RuntimeException("Not implemented");
	}

	public void remove() {
		dataLayer.removeSubscriptionPoint(this);

		// TODO: block all further activity
	}

	public Status getSubscriptionState(Key key) {
		SubscriptionImpl<N> s = dataLayer.subscriptions.get(key);

		if (s == null)
			return null;

		return s.getStatus();
	}

	@Override
	public String toString() {
		return "SP(" + dataLayer.nodeType + ") " + subscriber + " " + pattern
				+ " ";
	}
}
